package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**角色管理控制层
 * @author YH
 * @date 2020/4/16 14:33
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //分页查询角色列表
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            if (queryPageBean == null){
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(10);
            }
            PageResult pageResult = roleService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据角色id查询关联的菜单和权限
    @RequestMapping(value = "/findRoleIdByPermission")
    public Result findRoleIdByPermission(Integer roleId){
        try {
            List<Map<String, Object>> list = roleService.findRoleIdByPermission(roleId);
            return new Result(true,MessageConstant.GET_PERMISSION_LIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_LIST_FAIL);
        }
    }

    //根据角色id删除角色
    @RequestMapping(value = "/deleteById")
    public Result findDeleteByIdRole(Integer roleId){
        try {
            //调用服务删除角色
            roleService.findDeleteByIdRole(roleId);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (RuntimeException r){
            r.printStackTrace();
            return new Result(false,r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //新增角色
    @RequestMapping(value = "/add")
    public Result add(Integer[] menuIds, Integer[] permissionIds,@RequestBody Role role){
        try {
            roleService.add(menuIds,permissionIds,role);
            return new Result(true,MessageConstant.INSERT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.INSERT_ROLE_FAIL);
        }
    }

    //查询所有角色信息
    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<Role> roleList = roleService.findAll();

            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色id获取角色信息
    @RequestMapping(value = "/findById")
    public Result findById(Integer roleId){
        try {
            Role role = roleService.findById(roleId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色id查询角色关联的菜单id
    @RequestMapping(value = "/findRoleByMenu")
    public Result findRoleByMenu(Integer roleId){
        try {
            List<Integer> menuIds = roleService.findRoleByMenu(roleId);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //根据角色id查询角色关联的权限id
    @RequestMapping(value = "/findRoleByPermission")
    public Result findRoleByPermission(Integer roleId){
        try {
            List<Integer> permissionIds = roleService.findRoleByPermission(roleId);
            return new Result(true,MessageConstant.GET_PERMISSION_LIST_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PERMISSION_LIST_FAIL);
        }
    }

    //编辑角色信息
    @RequestMapping(value = "/edit")
    public Result edit(Integer[] menuIds,Integer[] permissionIds,@RequestBody Role role){
        try {
            roleService.edit(menuIds,permissionIds,role);
            return new Result(true,MessageConstant.EDOT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //查询关键字是否唯一
    @RequestMapping(value = "/findRoleKeyWordBycount")
    public Result findRoleKeyWordBycount(String keyword){
        Integer keyWordBycount = roleService.findRoleKeyWordBycount(keyword);
        if (keyWordBycount > 0){
            return new Result(false,MessageConstant.KEYWORD_ALL_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.KEYWORD_ALL_ROLE_SUCCESS);
    }

}
